<?php
/*
Plugin Name: sulb_share
Tags: share, facebook, buzz
Requires: wordpress >= 2.5
Tested on: 3.0
Version: 1.0
Author: RK
Description: Display share buttons in wordpress.
*/

load_plugin_textdomain( 'sulb_share' , false, 'sulb_share');
function ___($str)
{
	return __($str, 'sulb_share');
}

global $sulb_share_networks;
$sulb_share_networks = array(
	'buzz' => array('http://www.google.com/reader/link?url={url}&amp;title={title}&amp;srcURL={server_name}', ___("share on Buzz"), ___("buzz icon")),
	'twitter' => array('http://twitter.com/home?status={title}%20{url}', ___("tweet this"), ___("twitter icon")),
	'facebook' => array('http://www.facebook.com/sharer.php?u={url}', ___("share on Facebook"), ___("facebook icon")),
	'digg' => array('http://digg.com/submit?phase=2&url={url}&amp;title={title}', ___("share on Digg"), ___("Digg icon")),
	'stumbleupon' => array('http://stumbleupon.com/submit?url={url}&amp;title={title}', ___("share on Stumbleupon"), ___("Stumbleupon icon")),
	'delicious'  => array('http://del.icio.us/post?url={url}&amp;title={title}', ___("share on Del.icio.us"), ___("Del.icio.us icon")),
	'friendfeed'  => array('http://friendfeed.com/share?url={url}&amp;title={title}', ___("share on FriendFeed"), ___("FriendFeed icon")),
	'mrwong' => array('http://www.mister-wong.com/index.php?action=addurl&amp;bm_url={url}&amp;bm_description={title}', ___("share on mr. Wong"), ___("mr. Wong icon")),
	'xing' => array('https://www.xing.com/app/user?op=share;url={url};title={title};provider={blog_name}', ___("share on Xing"), ___("Xing icon")),
        'google_plus1' => array('https://plusone.google.com/_/+1/confirm?hl=en&url={url}',  ___("+1 on Google"), ___("Google +1 icon"))
);

function sulb_share_init() {
    if (!is_admin()) {
		wp_enqueue_style( 'sulb_share_css', get_bloginfo ( 'home' ).'/wp-content/plugins/sulb_share/sulb_share.css');
    }
}    

function sulb_share_admin()
{
	add_options_page(___('sulb_share'), ___('sulb_share'), 'level_10', 'sulb_share_options', 'sulb_share_options');
}

function sulb_share_options()
{
	global $sulb_share_networks;
	$options =  get_option('sulb_share_networs', array());
	if(isset($_REQUEST['sulb_share']))
	{
		foreach ($sulb_share_networks as $name => $optarr)
		{
			if($_REQUEST['share_network'][$name] == $name)
			{
				$options[$name][0] = true;
			}
			else
			{
				$options[$name][0] = false;
			}
		}
		update_option('sulb_share_networs', $options );
	}
	?>
	<form action="options-general.php" method="get">
	<input type="hidden" name="page" value="sulb_share_options"/>
	<?
	echo '<h3>'.___('Networks to use') .'</h3>';
	foreach ($sulb_share_networks as $name => $optarr)
	{
		?>
		<input type="checkbox" name="share_network[<?php echo $name;?>]" value="<?php echo $name;?>"
		<?php
		if($options[$name][0])
		{
			echo " checked='checked'";
		}
		?>
		/><?php echo $name;?><br/><?php
	}
	?>
	<input type="submit" name="sulb_share"/>
	</form>
	<?
}

function replace_shareurl($url, $myurl, $title)
{
	$url = str_replace('{url}', $myurl, $url);
	$url = str_replace('{title}', $title, $url);
	$url = str_replace('{blog_name}', urlencode(get_bloginfo('name')), $url);
	$url = str_replace('{server_name}', $_SERVER['SERVER_NAME'], $url);
	return $url;
}

function sulb_share_buttons($content)
{
	global $post;
	$c = '';
	if(!is_home() && !is_feed())
	{
		$myurl = urlencode('http://'.$_SERVER['SERVER_NAME'].str_replace('//', '/', '/'.$_SERVER['REQUEST_URI']));
		$title = urlencode($post->post_title);
		
		$c = "<div id='sulb_socialize'><div id='sulb_share'>".___('share with others')."</div><div id='sulb_sicons'>";
		global $sulb_share_networks;
		$options =  get_option('sulb_share_networs', array());
		foreach($sulb_share_networks as $name => $opt)
		{
			if($options[$name][0])
			{
				$c .= "<a href='".replace_shareurl($opt[0], $myurl, $title)."' id='".$name."' title='".$opt[1]."'
						><img src='".get_bloginfo ( 'home' )."/wp-content/plugins/sulb_share/".$name.".png'  alt='".$opt[2]."' width='32' height='32' /></a>";
			}
		}
		$c .= "</div><div id='share_icons_creator'>".___("icons by http://www.wpzoom.com")."</div></div>";
	}
	return $content.$c;
}

add_action('init', 'sulb_share_init');
add_filter('the_content' , 'sulb_share_buttons');
add_action('admin_menu', 'sulb_share_admin');
?>
